package com.etc.traval.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.etc.traval.dao.SellerDao;
import com.etc.traval.util.JDBCUtil;
import com.etc.travel.entity.Seller;
import com.etc.travel.entity.User;

import oracle.jdbc.OracleTypes;

public class SellerDaoImpl implements SellerDao {

	JDBCUtil util = new JDBCUtil();

	/**
	 * 商家登录的方法
	 * 
	 * @author 叶圣莹
	 * @param name商家名字
	 *            pwd商家密码
	 * @return Seller 商家信息
	 */

	@Override
	public Seller checkLogin(int id, String pwd) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj = util.callHasReturn("{call proc_checkLoginseller(?,?,?)}", list, id, pwd);
		ResultSet rs = (ResultSet) obj[0];
		Seller seller = null;
		try {
			if (rs.next()) {
				seller = new Seller();
				seller.setSeller_id(rs.getInt("seller_id"));
				seller.setSeller_name(rs.getString("seller_name"));
				seller.setSeller_pwd(rs.getString("seller_pwd"));
				seller.setSeller_money(rs.getDouble("seller_money"));
				seller.setSeller_addr(rs.getString("seller_addr"));
				seller.setSeller_tel(rs.getString("seller_tel"));
				seller.setSeller_logo(rs.getString("seller_logo"));
				seller.setSeller_synopsis(rs.getString("seller_synopsis"));
			}

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return seller;
	}

	/**
	 * 商家注册的方法
	 * 
	 * @author 叶圣莹
	 * @param 注册用户的信息
	 * @return 返回该注册用户的信息
	 */
	@Override
	public boolean AddSeller(Seller seller) {
		int i = util.update(
				"insert into t_seller(seller_name,seller_pwd,seller_money,seller_addr,seller_tel,seller_logo,seller_synopsis) values (?,?,?,?,?,?,?)",
				seller.getSeller_name(), seller.getSeller_pwd(), seller.getSeller_money(), seller.getSeller_addr(),
				seller.getSeller_tel(), seller.getSeller_logo(), seller.getSeller_synopsis());
		return i > 0;
	}

	/**
	 * 商家修改密码的方法
	 * 
	 * @author 叶圣莹
	 * @param 用户名
	 * @param 原密码
	 * @param 新密码
	 * @return 是否修改成功
	 */
	@Override
	public boolean updatePwd(String pwd, int id) {
		int i = util.update("update t_seller set  seller_pwd=? where seller_id=?", pwd, id);
		return i > 0;
	}

	/**
	 * 查找所有商家的方法
	 * 
	 * @author 叶圣莹
	 * @return 所有商家信息
	 */
	@Override
	public List<Seller> queryAll(int page, int size) {
		// 分页显示
		ArrayList<Seller> list2 = new ArrayList<Seller>();
		ResultSet rs = util.query(
				"select t.*,t.a from (select s.*,rownum as a  from (select * from t_seller) s where rownum<=?) t where t.a>?",
				(page * size), ((page - 1) * size));
		ArrayList<Seller> list = new ArrayList<Seller>();
		Seller seller = null;
		try {
			while (rs.next()) {
				seller = new Seller();
				seller.setSeller_id(rs.getInt("seller_id"));
				seller.setSeller_name(rs.getString("seller_name"));
				seller.setSeller_pwd(rs.getString("seller_pwd"));
				seller.setSeller_money(rs.getDouble("seller_money"));
				seller.setSeller_addr(rs.getString("seller_addr"));
				seller.setSeller_tel(rs.getString("seller_tel"));
				seller.setSeller_logo(rs.getString("seller_logo"));
				seller.setSeller_synopsis(rs.getString("seller_synopsis"));
				seller.setSeller_register(rs.getTimestamp("seller_register"));
				list2.add(seller);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			util.closeAll();
		}
		return list2;
	}

	/**
	 * 根据商家的ID查找用户信息。
	 * 
	 * @author 叶圣莹
	 * @return 商家信息
	 */
	@Override
	public Seller selectSellerById(int id) {
		ResultSet rs = util.query("select * from t_seller where seller_id=?", id);
		Seller seller = null;
		try {
			if (rs.next()) {
				seller = new Seller();
				seller.setSeller_id(rs.getInt("seller_id"));
				seller.setSeller_name(rs.getString("seller_name"));
				seller.setSeller_pwd(rs.getString("seller_pwd"));
				seller.setSeller_money(rs.getDouble("seller_money"));
				seller.setSeller_addr(rs.getString("seller_addr"));
				seller.setSeller_tel(rs.getString("seller_tel"));
				seller.setSeller_logo(rs.getString("seller_logo"));
				seller.setSeller_synopsis(rs.getString("seller_synopsis"));
			}

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return seller;
	}

	/**
	 * 根据名字关键字模糊查询店家的方法（分页显示）
	 * 
	 * @author cjw
	 * @param namekey
	 *            商家名字的关键字
	 * @param size
	 *            要显示的条数
	 * @param page
	 *            要显示的页数
	 */
	@Override
	public List<Seller> queryAllByNameKey(String namekey, int size, int page) {
		List<Seller> list2 = new ArrayList<Seller>();
		List<Integer> list = new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj = util.callHasReturn("{call proc_queryAllByNameKey(?,?,?,?)}", list, size, page, namekey);
		ResultSet rs = (ResultSet) obj[0];
		try {
			while (rs.next()) {
				Seller seller = new Seller();
				seller.setSeller_id(rs.getInt("seller_id"));
				seller.setSeller_name(rs.getString("seller_name"));
				seller.setSeller_pwd(rs.getString("seller_pwd"));
				seller.setSeller_money(rs.getDouble("seller_money"));
				seller.setSeller_addr(rs.getString("seller_addr"));
				seller.setSeller_tel(rs.getString("seller_tel"));
				seller.setSeller_logo(rs.getString("seller_logo"));
				seller.setSeller_synopsis(rs.getString("seller_synopsis"));
				list2.add(seller);
			}

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list2;
	}

	/**
	 * 查询所有商家总数
	 * 
	 * @author 叶圣莹
	 * @return num 商家总数
	 */
	@Override
	public int querySellerCount() {
		ResultSet rs = util.query("select count(*) as sellernum from t_seller");
		int num = 0;
		try {
			if (rs.next()) {
				num = rs.getInt("sellernum");
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			util.closeAll();
		}
		return num;
	}

	// 通过电话查询电话
	@Override
	public Seller querySellerBytel(String tel) {
		ResultSet rs = util.query("select * from t_seller where seller_tel=?", tel);
		Seller seller = null;
		try {
			if (rs.next()) {
				seller = new Seller();
				seller.setSeller_id(rs.getInt("seller_id"));
				seller.setSeller_name(rs.getString("seller_name"));
				seller.setSeller_pwd(rs.getString("seller_pwd"));
				seller.setSeller_money(rs.getDouble("seller_money"));
				seller.setSeller_addr(rs.getString("seller_addr"));
				seller.setSeller_tel(rs.getString("seller_tel"));
				seller.setSeller_logo(rs.getString("seller_logo"));
				seller.setSeller_synopsis(rs.getString("seller_synopsis"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}
		return seller;
	}

	// 通过电话查询商家是否存在
	@Override
	public int checksellertel(String tel) {
		ResultSet rs = util.query("select seller_name  from t_seller where seller_tel = ?", tel);
		String t = null;
		try {
			if (rs.next()) {
				t = rs.getString("seller_name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			util.closeAll();
		}
		if (t == null) {
			return 0;
		} else {
			return 1;
		}

	}

	/**
	 * @author 刘一清
	 * @param 地址关键字
	 * @return seller集合
	 */
	@Override
	public List<Seller> queryAllByAddkey(String addkey) {
		ResultSet rs = util.query("select * from t_seller where seller_addr like ?", "%" + addkey + "%");
		List<Seller> list = new ArrayList<Seller>();
		try {
			while (rs.next()) {
				Seller seller = new Seller();
				seller.setSeller_id(rs.getInt("seller_id"));
				seller.setSeller_name(rs.getString("seller_name"));
				seller.setSeller_pwd(rs.getString("seller_pwd"));
				seller.setSeller_money(rs.getDouble("seller_money"));
				seller.setSeller_addr(rs.getString("seller_addr"));
				seller.setSeller_tel(rs.getString("seller_tel"));
				seller.setSeller_logo(rs.getString("seller_logo"));
				seller.setSeller_synopsis(rs.getString("seller_synopsis"));
				list.add(seller);
			}

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * @author 刘一清
	 * @param seller的各个属性
	 * @return true false
	 */
	@Override
	public boolean updateAllById(String name, String addr, String tel, String synopsis, int id) {
		return util.update(
				"update t_seller set seller_name=?,seller_addr=?,seller_tel=?,seller_synopsis=? where seller_id=?", name,
				addr, tel, synopsis, id) > 0;

	}

	@Override
	public boolean rechargeMoneyById(Double money, int id) {
		// TODO Auto-generated method stub
		return util.update("update t_seller set seller_money=seller_money+? where seller_id=?", money, id) > 0;
	}

}
